﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetComponentsDx.Base.Classes;
using DevExpress.XtraEditors;
using ProgNetComponents.Design.Attributes;
using System.ComponentModel;
using DevExpress.XtraEditors.Repository;

namespace ProgNetComponentsDx.Components
{
    public class BaseEditFormatSettings : BaseSettings
    {
        public BaseEditFormatSettings(object target, BaseEditFormatProvider provider)
            : base(target, provider)
        {
            if (target is RepositoryItemButtonEdit ||
                target is ButtonEdit)
                ClearOnDelete = true;
        }

        [AlwaysSerialize]
        [NotifyParentProperty(true)]
        public bool ClearOnDelete
        {
            get;
            set;
        }
    }

    public class BaseEditFormatProvider : Base.Components.BaseExtenderProvider<BaseEditFormatSettings>
    {
        public BaseEditFormatProvider()
            : base()
        {

        }
        public BaseEditFormatProvider(IContainer container)
            : base(container)
        {

        }
        protected override void ProcessObject(object target, BaseEditFormatSettings settings)
        {
            RepositoryItemTextEdit rep = null;
            if (target is RepositoryItemTextEdit)
                rep = target as RepositoryItemTextEdit;
            else if (target is TextEdit)
            rep = (target as TextEdit).Properties;

            if (rep != null)
            {
                rep.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
                if (!this.IsDesignMode())
                {
                    rep.FormatEditValue -= new DevExpress.XtraEditors.Controls.ConvertEditValueEventHandler(rep_FormatEditValue);
                    rep.ParseEditValue -= new DevExpress.XtraEditors.Controls.ConvertEditValueEventHandler(rep_ParseEditValue);

                    rep.FormatEditValue += new DevExpress.XtraEditors.Controls.ConvertEditValueEventHandler(rep_FormatEditValue);
                    rep.ParseEditValue += new DevExpress.XtraEditors.Controls.ConvertEditValueEventHandler(rep_ParseEditValue);

                    if (settings.ClearOnDelete)
                    {
                        rep.KeyDown += new System.Windows.Forms.KeyEventHandler(rep_KeyDown);
                    }
                    else
                    {
                        rep.KeyDown -= new System.Windows.Forms.KeyEventHandler(rep_KeyDown);
                    }
                }
                if (rep.NullText == "[EditValue is null]")
                    rep.NullText = "";
            }
        }

        void rep_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Delete)
            {
                BaseEdit ed = sender as BaseEdit;
                if (ed.Properties.ReadOnly)
                    return;
                ed.EditValue = DBNull.Value;
            }
        }

        private void rep_ParseEditValue(object sender, DevExpress.XtraEditors.Controls.ConvertEditValueEventArgs e)
        {
            if (e.Value.IsNull())
                e.Value = DBNull.Value;
        }

        private void rep_FormatEditValue(object sender, DevExpress.XtraEditors.Controls.ConvertEditValueEventArgs e)
        {
            if (e.Value.IsNull())
                e.Value = DBNull.Value;
        }

        protected override BaseEditFormatSettings CreateSettings(object target)
        {
            return new BaseEditFormatSettings(target, this);
        }

        public override bool CanExtend(object extendee)
        {
            if (extendee is TextEdit ||
                extendee is RepositoryItemTextEdit)
                return true;
            return false;
        }
    }
}
